﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 简单了解Graphics操作;

namespace 简单了解Graphics操作
{
    public partial class Form6 : Form
    {
        private Tank _tank;
        public Form6()
        {
            InitializeComponent();
            _tank = new Tank(100, 100, 10, Direction.UP);
        }

        private void Form6_Paint(object sender, PaintEventArgs e)
        {
            _tank.DrawTank(this.CreateGraphics());
        }

        private void Form6_KeyDown(object sender, KeyEventArgs e)
        {
            //获取当前键盘按下哪个键
            Keys keyCode = e.KeyCode;
            switch (keyCode.ToString())
            {
                case "W":
                case "Up":
                    _tank.Direction = Direction.UP;
                    break;
                case "A":
                case "Left":
                    _tank.Direction = Direction.LEFT;
                    break;
                case "S":
                case "Down":
                    _tank.Direction = Direction.DOWN;
                    break;
                case "D":
                case "Right":
                    _tank.Direction = Direction.RIGHT;
                    break;
                default:
                    return;
            }
            timer1.Interval = 100;
            //开启计时器
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //调用坦克移动
            _tank.Move(this);
            //强制触发 Paint 屏幕重新绘制事件
            this.Invalidate();
        }
    }
}
 